package com.hx.vending.service.impl;

import com.hx.vending.bean.AdminTokenModel;
import com.hx.vending.bean.config.Constants;
import com.hx.vending.bean.entity.TokenModel;
import com.hx.vending.service.ITokenService;
import com.hx.vending.util.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/11/28.
 */
@Service
public class TokenServiceImpl implements ITokenService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String createToken(int userId) {
        //使用UUID作为源token
        String userid = String.valueOf(userId);
        String str = userid+"_"+UUID.randomUUID().toString().replace("-", "");
        String token = Base64Util.baseEncode(str);
        /*TokenModel model = new TokenModel(userid, token);*/
        //存储到redis并设置过期时间
        redisTemplate.boundValueOps(userid).set(token, Constants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
        return token;
    }

    @Override
    public TokenModel getToken(String authentication) {
        if (authentication == null || authentication.length() == 0) {
            return null;
        }
        String str = Base64Util.baseDecoder(authentication);
        String[] param = str.split("_");
        if (param.length != 2) {
            return null;
        }
//        使用userId和源token简单拼接成的token，可以增加加密措施
        String userId = param[0];
        String token = param[1];
        return new TokenModel(userId, token);
    }

    @Override
    public AdminTokenModel getAdminToken(String accredit) {
        if (accredit == null || accredit.length() == 0) {
            return null;
        }
        String str = Base64Util.baseDecoder(accredit);
        String[] param = str.split("_");
        if (param.length != 3) {
            return null;
        }
//        使用userId和源token简单拼接成的token，可以增加加密措施
        String adminId = param[0];
        String adminAccount = param[1];
        String token = param[2];
        return new AdminTokenModel(adminId,adminAccount, token);
    }

    @Override
    public void deleteToken(Integer userId) {
        redisTemplate.delete(String.valueOf(userId));
    }

    @Override
    public boolean checkToken(TokenModel model) {
        if (model == null) {
            return false;
        }
        String token = redisTemplate.boundValueOps(model.getUserId()).get();
        String str = Base64Util.baseEncode(model.getUserId()+"_"+model.getToken());
        if (token == null || !token.equals(str)) {
            return false;
        }
        //如果验证成功，说明此用户进行了一次有效操作，延长token的过期时间
        redisTemplate.boundValueOps(model.getUserId()).expire(Constants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
        return true;
    }

    @Override
    public boolean checkAdminToken(AdminTokenModel model) {
        if (model == null) {
            return false;
        }
        String token = redisTemplate.boundValueOps(model.getAdminId()+model.getAdminAccount()).get();
        String str = Base64Util.baseEncode(model.getAdminId()+"_"+model.getAdminAccount()+"_"+model.getToken());
        if (token == null || !str.equals(token)) {
            return false;
        }
        //        如果验证成功，说明此用户进行了一次有效操作，延长token的过期时间
        redisTemplate.boundValueOps(model.getToken()).expire(Constants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
        return true;
    }


}
